﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using UIElements.Service;
using System.Windows.Input;
using System.Windows;

namespace UIElements.ViewModel
{
    public class PatternTemplateViewModel : TreeViewItemViewModel
    {
        #region Constructors

        public PatternTemplateViewModel()
            : base()
        {
            // initialize commands
            this.addPatternInstanceCmd = new AddPatternInstanceCommand(this);
        }

        #endregion

        #region Properties

        private int patternTemplateID;
        public int PatternTemplateID
        {
            get { return patternTemplateID; }
            set
            {
                patternTemplateID = value;
                OnPropertyChanged("PatternTemplateID");
            }
        }

        private string patternTemplateName;
        public string PatternTemplateName
        {
            get { return patternTemplateName; }
            set
            {
                patternTemplateName = value;
                OnPropertyChanged("PatternTemplateName");
            }
        }

        private string templateComments;
        public string TemplateComments
        {
            get { return templateComments; }
            set
            {
                templateComments = value;
                OnPropertyChanged("TemplateComments");
                //dataAccess.UpdateTemplateComment(patternTemplateID, templateComments);
            }
        }

        private ICommand addPatternInstanceCmd;
        public ICommand AddPatternInstanceCmd
        {
            get { return addPatternInstanceCmd; }
        }

        #endregion

        #region Public Methods

        public override bool SaveChanges()
        {
            // save changes to DB
            var successful = dataAccess.SavePatternTemplate(patternTemplateID, name: patternTemplateName, comments: templateComments);
            if (!successful)
            {
                return false;
            }

            // update names of physical files
            UpdatePhysicalFileNames();

            return true;
        }

        /// <summary>
        /// get the table of properties of this pattern model
        /// </summary>
        /// <returns></returns>
        public override DataTable GetProperties()
        {
            // create new data table
            DataTable dtProperties = new DataTable();

            // create property name column
            DataColumn colPropertyName = new DataColumn("Property", typeof(string));
            dtProperties.Columns.Add(colPropertyName);

            // create property value column
            DataColumn colPropertyValue = new DataColumn("Value", typeof(string));
            dtProperties.Columns.Add(colPropertyValue);

            // add name row
            DataRow rowTemplateName = dtProperties.NewRow();
            dtProperties.Rows.Add(rowTemplateName);
            rowTemplateName["Property"] = "Pattern Name";
            rowTemplateName["Value"] = patternTemplateName;

            // add comments row
            DataRow rowTemplateComments = dtProperties.NewRow();
            dtProperties.Rows.Add(rowTemplateComments);
            rowTemplateComments["Property"] = "Comments";
            rowTemplateComments["Value"] = templateComments;

            // return the data table of properties
            return dtProperties;
        }

        #endregion

        #region Helpers

        private void UpdatePhysicalFileNames()
        {
            // get the old path to the template directory
            var oldTemplateDirectory = IOService.GetFullPath(@"PatternManagementExtension\" + PreviousName);

            // get the new path to the instance directory
            var newTemplateDirectory = IOService.GetFullPath(@"PatternManagementExtension\" + patternTemplateName);

            // update the name of the pattern instance directory
            IOService.RenameDirectory(oldTemplateDirectory, newTemplateDirectory);

            // rename the x-frame file
            var oldTemplateXFrameFilePath = IOService.GetCombinedPath(newTemplateDirectory, (PreviousName + @".xvcl"));
            var newTemplateXFrameFilePath = IOService.GetPatternTemplateFilePath(patternTemplateName, PatternFileType.XFrame);
            IOService.RenameFile(oldTemplateXFrameFilePath, newTemplateXFrameFilePath);

            // reset the values for x-frame file
            IOService.SetValue(newTemplateXFrameFilePath, "x-frame", "name", patternTemplateName);

            // rename the template file
            var oldTemplateTemplateFilePath = IOService.GetCombinedPath(newTemplateDirectory, (PreviousName + @".template"));
            var newTemplateTemplateFilePath = IOService.GetPatternTemplateFilePath(patternTemplateName, PatternFileType.Template);
            IOService.RenameFile(oldTemplateTemplateFilePath, newTemplateTemplateFilePath);

            // rename the config file
            var oldTemplateConfigFilePath = IOService.GetCombinedPath(newTemplateDirectory, (PreviousName + @".config"));
            var newTemplateConfigFilePath = IOService.GetPatternTemplateFilePath(patternTemplateName, PatternFileType.SPC);
            IOService.RenameFile(oldTemplateConfigFilePath, newTemplateConfigFilePath);

            // reset the values for the config file
            IOService.SetValue(newTemplateConfigFilePath, "x-frame", "name", patternTemplateName);
            IOService.SetValue(newTemplateConfigFilePath, "adapt", "x-frame", patternTemplateName + ".xvcl");
        }

        #endregion

        #region Commands

        #region Add New Pattern Instance

        private void AddNewPatternInstance()
        {
            MessageBox.Show("Command executed successfully!");
        }

        public class AddPatternInstanceCommand : ICommand
        {
            private PatternTemplateViewModel viewModel;

            public AddPatternInstanceCommand(PatternTemplateViewModel viewModel)
            {
                this.viewModel = viewModel;
                this.viewModel.PropertyChanged += (sender, e) =>
                {
                    if (this.CanExecuteChanged != null)
                    {
                        this.CanExecuteChanged(this, new EventArgs());
                    }
                };
            }

            public bool CanExecute(object parameter)
            {
                //return viewModel.PatternTemplates.Count != 0;
                return true;
            }

            public event EventHandler CanExecuteChanged;

            public void Execute(object parameter)
            {
                this.viewModel.AddNewPatternInstance();
            }
        }

        #endregion

        #endregion
    }
}
